<?php
include ("Pattern.php");
include ("../models/DBConnection.php");

class Kmeans {

    var $_centroids;
    var $_patterns;
    var $_k;
    var $_times;
    var $_clusters;
    var $GLOBAL_DATA;

    function Kmeans($__k = 3, $_times = 3) {
        srand(time());
        $this->_k = $__k;
        $this->_patterns = array();
        $this->_centroids = array();
        $this->_clusters = array();
        $this->GLOBAL_DATA = array();

        $query = new query("select * from  features");
        foreach ($query->v as $reg) {
            $pattern = new Pattern("$reg->titulo");
            $pattern->_pattern[] = ($reg->bitrate == '') ? 0 : $reg->bitrate;
            $pattern->_pattern[] = ($reg->vbitrate == '') ? 0 : $reg->vbitrate;
            $pattern->_pattern[] = ($reg->abitrate == '') ? 0 : $reg->abitrate;
            $pattern->_pattern[] = ($reg->framecount == '') ? 0 : $reg->framecount;
            $pattern->_pattern[] = ($reg->framerate == '') ? 0 : $reg->framerate;
            $pattern->_pattern[] = ($reg->framewidth == '') ? 0 : $reg->framewidth;
            $pattern->_pattern[] = ($reg->frameheight == '') ? 0 : $reg->frameheight;
            $pattern->_pattern[] = ($reg->audiochannels == '') ? 0 : $reg->audiochannels;
//			echo $pattern->_label . " ";
//			for ($i = 0; $i < count($pattern->_pattern); $i++)
//				echo $pattern->_pattern[$i] . " ";
//			echo "</br>";	
            $this->_patterns[] = $pattern;
        }
        // Making Kmeans
        $counter = 0;
        $this->__centroid_selection();
        while ($counter < $_times) {
            echo "</br>Iteration " . $counter . "</br>";
            $this->__evolution();
            $this->__centroid_updating();
            $counter++;
        }
    }

    function __centroid_selection() {
        
        $rand = rand(0, count($this->_patterns));
        $counter = 0;
        while ($counter < $this->_k) {
            while ($this->_patterns[$rand]->_selected == true)
                $rand = rand(0, count($this->_patterns));
            $this->_patterns[$rand]->_selected = true;
            $this->_centroids[] = $this->_patterns[$rand];
            $counter++;
        }
        for ($i = 0; $i < $this->_k; $i++) {
            for ($k = 0; $k < count($this->_centroids[$i]->_pattern); $k++)
                echo $this->_centroids[$i]->_pattern[$k] . " ";
            echo "</br>";
        }
    }

    function __evolution() {
        unset($this->_clusters);
        for ($i = 0; $i < count($this->_patterns); $i++) {
            $index = 0;
            $min_dist = $this->_patterns[$i]->euclidean_distance($this->_centroids[$index]->_pattern);
            for ($k = 1; $k < $this->_k; $k++) {
                $new_dist = $this->_patterns[$i]->euclidean_distance($this->_centroids[$k]->_pattern);
                if ($new_dist < $min_dist) {
                    $min_dist = $new_dist;
                    $index = $k;
                }
            }
            $this->_clusters[$index][] = $this->_patterns[$i];
        }

        for ($k = 0; $k < $this->_k; $k++) {
            echo count($this->_clusters[$k]) . "</br>";
        }
    }

    function __centroid_updating() {
        unset($this->GLOBAL_DATA);
        for ($k = 0; $k < $this->_k; $k++) {
            $pattern = new Pattern();
            for ($i = 0; $i < count($this->_centroids[0]->_pattern); $i++)
                $pattern->_pattern[] = 0.0;
            for ($i = 0; $i < count($this->_clusters[$k]); $i++) {
                for ($t = 0; $t < count($this->_clusters[$k][$i]->_pattern); $t++) {
                    $pattern->_pattern[$t] += $this->_clusters[$k][$i]->_pattern[$t];
                }
            }
            for ($i = 0; $i < count($this->_centroids[0]->_pattern); $i++) {
                $pattern->_pattern[$i] /= count($this->_clusters[$k]);
                $this->GLOBAL_DATA[$k][] = $pattern->_pattern[$i];
                echo $pattern->_pattern[$i] . " ";
            }
            echo "</br>";
            $this->_centroids[$k] = $pattern;
        }
    }

}
$nCluster = 5;
$nIteraciones = 50;

$k = new Kmeans($nCluster, $nIteraciones);

echo "-------</br>";

for ($kk = 0; $kk < $nCluster; $kk++) {
    for ($i = 0; $i < 8; $i++) {
        echo $k->GLOBAL_DATA[$kk][$i] . " ";
    }
    echo "</br>";
}

for ($kk = 0; $kk < 8; $kk++) {
    $max = 0.0;
    for ($i = 0; $i < $nCluster; $i++) {
        $tmpMax = $k->GLOBAL_DATA[$i][$kk];
        if ($tmpMax > $max)
            $max = $tmpMax;
    }
    for ($i = 0; $i < $nCluster; $i++) {
        $tmpVal = $k->GLOBAL_DATA[$i][$kk];
        $tmp = ($tmpVal * 100) / $max;
        $k->GLOBAL_DATA[$i][$kk] = $tmp;
    }
}
echo "++++++++</br>";
for ($kk = 0; $kk < $nCluster; $kk++) {
    for ($i = 0; $i < 8; $i++) {
        echo $k->GLOBAL_DATA[$kk][$i] . " ";
    }
    echo "</br>";
}

$RGBs_clusters = array();
for ($kk = 0; $kk < $nCluster; $kk++) {
    $RGBs_clusters[$kk][0] = rand(0, 255);
    $RGBs_clusters[$kk][1] = rand(0, 255);
    $RGBs_clusters[$kk][2] = rand(0, 255);
}

?>
<!doctype html>
<html>
    <head>
        <title>Kmeans - Graf</title>
        <script src="../Controllers/js/jquery/Chart.js"></script>
        <meta name = "viewport" content = "initial-scale = 1, user-scalable = no">
        <style>
            canvas{
            }
            .chart-legend ul {
                list-style: none;
                //width: 600px;
                margin: 0px auto 0;
            }
            .chart-legend li {
                text-indent: 16px;
                line-height: 24px;
                position: relative;
                font-weight: 200;
                display: block;
                float: left;
                width: 50%;
                font-size: 0.8em;
            }
            .chart-legend  li:before {
                display: block;
                width: 10px;
                height: 16px;
                position: absolute;
                left: 0;
                top: 3px;
                content: "";
            }
            <?php
            for ( $kk = 0; $kk < $nCluster; $kk++){
                $tmp =$kk+1;
             echo ".cluster".$tmp.":before { background-color: rgb(".$RGBs_clusters[$kk][0].",".$RGBs_clusters[$kk][1].",".$RGBs_clusters[$kk][2].");}";
             echo "\n";
            }
            ?>
        </style>
    </head>
    <body>
        <div style="width: 600px; height: 600px;">
            <div>
                <canvas id="radarChart" height="450" width="450"></canvas>
                <script>

                            var radarChartData = {
                            labels: ["BitRate", "VideoBitRate", "AudioBitRate", "FrameCount", "FrameRate", "FrameWidth", "FrameHeight", "AudioChannels"],
                                    datasets: [

<?php
for ($kk = 0; $kk < 5; $kk++) {
    ?>
                                        {
                                        fillColor: <?php echo "\"rgba(" . $RGBs_clusters[$kk][0] . "," . $RGBs_clusters[$kk][1] . "," . $RGBs_clusters[$kk][2] . ",0.5)\"" ?>,
                                                strokeColor: <?php echo "\"rgba(" . $RGBs_clusters[$kk][0] . "," . $RGBs_clusters[$kk][1] . "," . $RGBs_clusters[$kk][2] . ",1)\"" ?>,
                                                pointColor: <?php echo "\"rgba(" . $RGBs_clusters[$kk][0] . "," . $RGBs_clusters[$kk][1] . "," . $RGBs_clusters[$kk][2] . ",1)\"" ?>,
                                                pointStrokeColor: "#fff",
                                                data: [
    <?php
    for ($i = 0; $i < 8; $i++) {
        if ($i == 7)
            echo $k->GLOBAL_DATA[$kk][$i];
        else
            echo $k->GLOBAL_DATA[$kk][$i] . ",";
    }
    ?>
                                                ]
                                        },
    <?php
}
?>
                                    ]
                            }

                    var myRadar = new Chart(document.getElementById("radarChart").getContext("2d")).Radar(radarChartData, {scaleShowLabels: false, pointLabelFontSize: 10});                        </script>
            </div>
            <div class="chart-legend">
                <ul>
                    <?php 
                    for ( $kk = 0; $kk < $nCluster; $kk++) {
                        $tmp = $kk+1;
                        echo "<li class=\"cluster".$tmp."\">";                         
                        echo "<i> Cluster " . $tmp ."</i>";
                        echo "</li>";
                    }
                    ?>
                </ul>
            </div>  
        </div>
        <canvas id="barChart" height="450" width="900"></canvas>



        <script>
                    var barChartData = {
                    labels : ["BitRate", "VideoBitRate", "AudioBitRate", "FrameCount", "FrameRate", "FrameWidth", "FrameHeight", "AudioChannels"],
                            datasets : [
<?php
for ($kk = 0; $kk < 5; $kk++) {
    ?>
                                {
                                fillColor: <?php echo "\"rgba(" . $RGBs_clusters[$kk][0] . "," . $RGBs_clusters[$kk][1] . "," . $RGBs_clusters[$kk][2] . ",0.5)\"" ?>,
                                        strokeColor: <?php echo "\"rgba(" . $RGBs_clusters[$kk][0] . "," . $RGBs_clusters[$kk][1] . "," . $RGBs_clusters[$kk][2] . ",1)\"" ?>,
                                        data: [
    <?php
    for ($i = 0; $i < 8; $i++) {
        if ($i == 7)
            echo $k->GLOBAL_DATA[$kk][$i];
        else
            echo $k->GLOBAL_DATA[$kk][$i] . ",";
    }
    ?>
                                        ]
                                },
    <?php
}
?>
                            ]

                    }

            var myLine = new Chart(document.getElementById("barChart").getContext("2d")).Bar(barChartData);
        </script>
    </body>
</html>
